<?php

namespace app\common\controller;
use app\admin\menuname;
use think\Request;
class Admin extends Common
{
    protected $kz = ['User'];
    protected $at = ['login'];
    public function initialize()
    {
        if(!session('login') && !in_array($this->request->controller(),$this->kz) && !in_array($this->request->action(),$this->at))
        {
            return $this->redirect('user/login');
        }else{
            $user = session('login');
            
            $this->assign('login',session('login'));
        }
        if(isset($user['qxurl'])){
            if($user['qxurl'] == 0){
                $menu = model('aa')->select()->toArray();
                $menu = getTree($menu,0);
            }elseif($user['qxurl'] == '-1'){
                $menu = 'no';
            }
            else{
                $menu = model('aa')->where('id','in',$user['qxurl'])->select()->toArray();
                $menu = getTree($menu,0);
            }
            $this->assign('menu',$menu);
        }else{
            $menu = model('aa')->select()->toArray();
            $menu = getTree($menu,0);
            $this->assign('menu',$menu);
            
        }
        // pr($menu);
        if(isset($menu[0]['pid'][0])){
        	// pr($menu[0]['pid'][0]['url']);
        	$this->assign('menuurl',$menu[0]['pid'][0]['url']);
        }
        $now = substr($_SERVER['REQUEST_URI'],1);
        $now = explode('.',$now);
        if(count($now) > 1){
            array_pop($now);
        }
        $now = implode($now,'');
        $now = model('aa')->where('url','=',$now)->find();
        if($now){
            $now = $now->toArray();
            if($now['pid'] !== 0){
                $fu = model('aa')->field('name,pid')->where('id','=',$now['pid'])->find()->toArray();
                $now['name'] = $fu['name'] . ' / ' . $now['name'];
                if($fu['pid'] !== 0){
                    $fus = model('aa')->field('name')->where('id','=',$now['pid'])->find()->toArray()['name'];
                    $now['name'] = $fus . ' / ' . $now['name'];
                }
            }
            $this->assign('menuname',$now['name']);
        }
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function status()
    {
        if($this->request->isPost()){
            $post = input('post.');
            if($post['status'] == 10){
                $post['status'] = 20;
            }else{
                $post['status'] = 10;
            }
            try{
                $model = model($this->request->controller())->save(['status'=>$post['status']],['id'=>$post['id']]);
            }catch (\Exception $e){
                return json_encode($e->getMessage());
            }
            return json_encode($model);
        }
    }
    public function index()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            if(isset($p['b']['a']))
            {
                echo 123;
            }else{
                echo '';
            }
            if(isset($post['class']))
            {
                if($post['class'] == 1){
                    $this->assign('iframe',url('product/group',['id'=>5,'index'=>2]));
                }elseif ($post['class'] == 2){
                    $this->assign('iframe',url('user/register',['id'=>6,'index'=>0]));
                }
            }
        }
        if($this->request->isGet())
        {
            $get = input('get.');
            if(isset($get['url'])){
                $url = explode('-',$get['url']);
                $kf = str_replace("_","/",$url[0]);
                // $idz = str_replace("_","=",$url[1]);
                // $indexz = str_replace("_","=",$url[2]);
                // $url = '/admin/' . $kf .'?'. $idz .'&'. $indexz;
                $url = '/admin/' . $kf;
                $this->assign('iframe',$url);
            }
        }
        return $this->fetch();
    }
    public function setting()
    {
        return $this->fetch();
    }
    //新增
    public function add()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            if($this->request->controller() == 'Product'){
                if(strstr($post['images'],'\\')){
                    $post['images'] = str_replace("\\","/",$post['images']);
                }
            }
            model($this->request->controller())->save($post);
            $id = model($this->request->controller())->id;
            if($this->request->controller() == 'Product'){
                if($post['dt']){
                    $dt = explode('%',$post['dt']);
                    array_pop($dt);
                    foreach ($dt as $v){
                        if(strstr($v,'\\')){
                            $v = str_replace("\\","/",$v);
                        }
                        $list[] = ['image'=>$v,'product_id'=>$id];
                    }
                    model('atlas')->saveAll($list);
                }
                $this->redirect('index');
            }
            if($this->request->controller() == 'Classs'){
                return $this->redirect('index/index');
            }
        }
        $list = model('classs')->select()->toArray();
        $this->assign('list',$list);
        return $this->fetch();
    }
    //修改
    public function update()
    {
        $get = input('get.');
        $id = $get['sid'];
        $list = model($this->request->controller())->get($id)->toArray();
        if($this->request->isPost())
        {
            $post = input('post.');
            if(!$post['images']){
                $post['images'] = $list['images'];
            }
            if($this->request->controller() == 'Product'){
                if(strstr($post['images'],'\\')){
                    $post['images'] = str_replace("\\","/",$post['images']);
                }
                $dt = explode('%',$post['dt']);
                array_pop($dt);
                foreach ($dt as $v){
                    if(strstr($v,'\\')){
                        $v = str_replace("\\","/",$v);
                    }
                    $lista[] = ['image'=>$v,'product_id'=>$id];
                }
                if(isset($lista)){
                    model('atlas')->saveAll($lista);
                }
            }
            model($this->request->controller())->save($post,['id'=>$id]);
            return $this->redirect('product/index',['id'=>5,'index'=>0]);
        }
        if($this->request->controller() == 'Product'){
            $list['dt'] = model('atlas')->where('product_id','=',$list['id'])->select()->toArray();
        }
        $this->assign('list',$list);
        $class= model('classs')->select()->toArray();
        $this->assign('class',$class);
        return $this->fetch();
    }
    public function view()
    {
        $get = input('get.');
        if(isset($get['battery_order'])){
            $id = $get['battery_order'];
            $list = model($this->request->controller())->where('id','=',$id)->with('gl')->find()->toArray();
            pr($list);
            $this->assign('list',$list);
        }
        return $this->fetch();
    }
    //封面添加
    public function images()
    {
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( 'upload/fm');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $info->getSaveName();
        }else{
            // 上传失败获取错误信息
            return json_encode($file->getError());
        }
        return json_encode('/upload/fm/' . $info->getSaveName());
    }
    public function uplete()
    {
        return $this->fetch();
    }
    public function delete()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            try{
                $model = model($this->request->controller())->destroy($post['id']);
            }catch (\Exception $e){
                return json_encode($e->getMessage());
            }
            return json_encode($model);
        }
    }
}
